# Maintainer: Jakub Jirutka <jakub@jirutka.cz>
# Contributor: Steve Holweg <skytep@gmail.com>
# Contributor: Baptiste Jonglez <baptiste--aur@jonglez.org>
# Contributor: Jakub Jirutka <jakub@jirutka.cz>
pkgname=kea
# When bumping, always rebuild all kea hooks (kea-* aports)!
pkgver=1.7.8
pkgrel=0
pkgdesc="DHCPv4 and DHCPv6 server from ISC"
url="http://kea.isc.org"
arch="all"
license="MPL2"
depends="kea-dhcp4 kea-dhcp6"
depends_dev="boost-dev log4cplus-dev"
checkdepends="procps"
makedepends="
	$depends_dev
	bison
	botan-dev
	flex
	libcap
	mariadb-dev
	postgresql-dev
	python3-dev
	"
pkgusers="kea"
pkggroups="kea"
install="$pkgname-common.pre-install"
subpackages="
	$pkgname-doc
	$pkgname-static
	$pkgname-dev
	$pkgname-admin::noarch
	$pkgname-ctrl-agent:ctrlagent
	$pkgname-dhcp-ddns:dhcpddns
	$pkgname-dhcp4
	$pkgname-dhcp6
	$pkgname-http
	$pkgname-shell::noarch
	$pkgname-hook-mysql-cb:hook_mysql_cb
	$pkgname-hook-ha:hook_ha
	$pkgname-hook-stat-cmds:hook_stat_cmds
	$pkgname-hook-lease-cmds:hook_lease_cmds
	$pkgname-hook-flex-option:hook_flex_option
	$pkgname-common
	"
source="https://ftp.isc.org/isc/$pkgname/$pkgver/$pkgname-$pkgver.tar.gz
	disable-db-tests.patch
	configs-fix-paths.patch
	use-runstatedir-for-pid-file-location.patch
	put-LOCKFILE_DIR-to-runstatedir.patch
	kea-admin-remove-builddir.patch
	kea.initd.in
	"
validpgpkeys="BE0E9748B718253A28BB89FFF1B11BF05CF02E57" # Internet Systems Consortium, Inc. (Signing key, 2017-2018) <codesign@isc.org>

# secfixes:
#   1.7.2-r0:
#     - CVE-2019-6472
#     - CVE-2019-6473
#     - CVE-2019-6474

prepare() {
	default_prepare

	# Remove the builddir path from the scripts.
	# NOTE: There's a new script each release, so using patch file would be
	# very inconvenient.
	find src/share/database/scripts/ \
		-name '*.sh.in' \
		-exec sed -i 's|^\s*. @abs_top_builddir@/src/bin/admin/admin-utils.sh.*|echo "admin-utils.sh not found!"; exit 1|' {} \;
}

build() {
	# Complete build for dev and doc

	./configure \
		--build=$CBUILD \
		--host=$CHOST \
		--prefix=/usr \
		--sysconfdir=/etc \
		--localstatedir=/var \
		--libexecdir=/usr/lib \
		--runstatedir=/run \
		--with-mysql \
		--with-pgsql \
		--disable-rpath \
		--enable-generate-messages \
		--enable-generate-parser \
		--enable-shell
	make
}

check() {
	make check
}

package() {
	make DESTDIR="$pkgdir" install

	cd "$pkgdir"

	# Allow kea to run under unpriviliged user.
	local name; for name in kea-dhcp-ddns kea-dhcp6; do
		setcap 'cap_net_bind_service=+ep' ./usr/sbin/$name
	done
	setcap 'cap_net_bind_service,cap_net_raw=+ep' ./usr/sbin/kea-dhcp4

	install -d -m 0750 -o kea -g kea ./var/log/kea ./var/lib/kea

	# keactrl is unnecessary since we provide OpenRC init scripts.
	rm ./usr/sbin/keactrl ./etc/kea/keactrl.conf

	rm -Rf ./run
}

dev() {
	default_dev

	amove ./usr/bin/kea-msg-compiler
}

admin() {
	pkgdesc="Databases administration tools for Kea DHCP server"
	depends="$pkgname-common=$pkgver-r$pkgrel"

	amove ./usr/sbin/kea-admin
	amove ./usr/share/kea/
}

ctrlagent() {
	pkgdesc="Kea Control Agent - REST service for controlling Kea DHCP server"
	depends="$pkgname-common=$pkgver-r$pkgrel"

	amove usr/sbin/kea-ctrl-agent
	amove ./etc/kea/kea-ctrl-agent.conf

	_install_initd kea-ctrl-agent
}

dhcpddns() {
	pkgdesc="Kea DHCP Dynamic DNS Server"
	depends="$pkgname-common=$pkgver-r$pkgrel"

	amove ./usr/sbin/kea-dhcp-ddns
	amove ./etc/kea/kea-dhcp-ddns.conf
	amove ./usr/lib/libkea-asiodns.so.*

	_install_initd kea-dhcp-ddns
}

dhcp4() {
	pkgdesc="Kea IPv4 DHCP Server"
	depends="$pkgname-common=$pkgver-r$pkgrel"

	amove ./usr/sbin/kea-dhcp4
	amove ./etc/kea/kea-dhcp4.conf

	_install_initd kea-dhcp4
}

dhcp6() {
	pkgdesc="Kea IPv6 DHCP Server"
	depends="$pkgname-common=$pkgver-r$pkgrel"

	amove ./usr/sbin/kea-dhcp6
	amove ./etc/kea/kea-dhcp6.conf

	_install_initd kea-dhcp6
}

hooks() {
	pkgdesc="Hooks libraries for Kea DHCP server"
	depends=""

	amove ./usr/lib/kea/hooks/
}

# Needed only by ctrl-agent and hooks.
http() {
	depends=""

	amove ./usr/lib/libkea-http.so*
}

shell() {
	pkgdesc="Text client for Kea DHCP Control Agent"
	depends="python3"

	amove ./usr/sbin/kea-shell
	amove ./usr/lib/python3*
}

hook_mysql_cb() {
	pkgdesc="Kea MySQL Configuration Backend hooks library"
	depends=""

	amove ./usr/lib/kea/hooks/libdhcp_mysql_cb.so
}

hook_ha() {
	pkgdesc="Kea High Availability hooks library"
	depends="$pkgname-hook-lease-cmds"

	amove ./usr/lib/kea/hooks/libdhcp_ha.so
}

hook_stat_cmds() {
	pkgdesc="Kea Statistics Commands hooks library"
	depends=""

	amove ./usr/lib/kea/hooks/libdhcp_stat_cmds.so
}

hook_lease_cmds() {
	pkgdesc="Kea Lease Commands hooks library"
	depends=""

	amove ./usr/lib/kea/hooks/libdhcp_lease_cmds.so
}

hook_flex_option() {
	pkgdesc="Kea Flexible Option hooks library"
	depends=""

	amove ./usr/lib/kea/hooks/libdhcp_flex_option.so
}

common() {
	pkgdesc="Common files and libraries for Kea DHCP Server"
	replaces="kea-utils"  # for backward compatibility
	provides="kea-utils=$pkgver-r$pkgrel"  # for backward compatibility
	depends=""

	mkdir -p "$subpkgdir"
	mv "$pkgdir"/* "$subpkgdir"/
}

_install_initd() {
	local name="$1"

	install -Dm755 "$srcdir"/kea.initd.in "$subpkgdir"/etc/init.d/$name
	sed -i "s|@@NAME@@|$name|g" "$subpkgdir"/etc/init.d/$name
}

sha512sums="1bed7a20f0ee2198eb6c196cf6895217fecea1a8274d155d669bf30ac1aab795917e7537d279bad55c951c3c99a7cd7cf9a86594bf6f7a689d1f694823353a28  kea-1.7.8.tar.gz
55780bbcbd79b81d55bdccd46da6ca6babe8b7894aa82948308dc4f8a0e2b5d33fad633276696c8ba7bd063026bb11ae529a096375d0572833c66e2132888775  disable-db-tests.patch
6d142d7ede2bd29a9133fe4e4a3ca6bab2b51a56926b268fab218921edd49250b240c0f59efa8a389b49a5478db5bda95acf531382b14cdf9327dddddcdb2d4d  configs-fix-paths.patch
c2472e5c4eebd604be4baeda991161c6174b73d6ff1e3971ecdd29d1ad8557c92cdcef1d2b935124c756caa298c01d51ec7e1951c5c4d1cf6ec7d853b2f3e4ac  use-runstatedir-for-pid-file-location.patch
062e6eb88c49aeeac5d78c37c43cda1ff753e19f833aaa1525e37f66eede819910d4849bd954f3ef8ceb1b13e747953c99e2a9a4d9755b6da8d82c4e08b2b55c  put-LOCKFILE_DIR-to-runstatedir.patch
5b93d96f2cbdce327ffceef5ace19ba5327a0c7d0d61714e35a0d72912ecbbec6a1864249612b48f8db20dffc3622170c3e21b1a67297fd834f47b94db85b9d7  kea-admin-remove-builddir.patch
4029a526a210cb49ca480fea728dfae5933371244df6348411081db1e8b23924cbbdde5fa745ab4c14552a95451953a18a65d7660227bc0bb1d51be708fab92f  kea.initd.in"
